package cn.exrick.controller;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.exrick.common.exception.NeedLoginException;
import cn.exrick.pojo.TbPreferential;
import cn.exrick.pojo.TbPreferentialItem;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.dto.ItemDto;
import cn.exrick.pojo.front.CartProduct;
import cn.exrick.pojo.front.Member;
import cn.exrick.service.CartService;
import cn.exrick.service.ItemService;
import cn.exrick.service.LoginService;
import cn.exrick.service.PreferentialItemService;
import cn.exrick.service.PreferentialService;
import cn.exrick.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(description = "购物车")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private PreferentialItemService preferentialItemService;
    @Autowired
    private PreferentialService preferentialService;

    @RequestMapping(value = "/member/addCart", method = RequestMethod.POST)
    @ApiOperation(value = "添加购物车商品")
    public Result<Object> addCart(String token, Long productId, Integer productNum) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        int result = cartService.addCart(member.getId(), productId, productNum);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/member/cartList", method = RequestMethod.POST)
    @ApiOperation(value = "获取购物车商品列表")
    public Result<List<CartProduct>> getCartList(String token) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        List<CartProduct> list = cartService.getCartList(member.getId());
        for(CartProduct cartProduct:list){
            ItemDto itemDto = itemService.getItemById(cartProduct.getProductId());
            if(itemDto!=null){
                cartProduct.setNum(itemDto.getNum());
                List<TbPreferentialItem> preferentialItems = preferentialItemService.findByItemId(itemDto.getId(),TbPreferentialItem.STATUS_ABLE);
                List<TbPreferential> preferentials = new ArrayList<>();
                for (TbPreferentialItem preferentialItem : preferentialItems){
                    TbPreferential preferential = preferentialService.findById(preferentialItem.getPreferentialId());
                    if(preferential!=null){
                        preferentials.add(preferential);
                    }
                }
                cartProduct.setPreferentials(preferentials);
            }
        }
        return new ResultUtil<List<CartProduct>>().setData(list);
    }

    @RequestMapping(value = "/member/cartEdit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑购物车商品")
    public Result<Object> updateCartNum(String token, Long productId, Integer productNum, String checked) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        int result = cartService.updateCartNum(member.getId(), productId, productNum, checked);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/member/editCheckAll", method = RequestMethod.POST)
    @ApiOperation(value = "是否全选购物车商品")
    public Result<Object> editCheckAll(String token, String checked) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        int result = cartService.checkAll(member.getId(), checked);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/member/cartDel", method = RequestMethod.POST)
    @ApiOperation(value = "删除购物车商品")
    public Result<Object> deleteCartItem(String token, String productIds) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        String[] productId = productIds.split(",");
        long[] itemIds = new long[productId.length];
        int i = 0;
        for (String pId : productId) {
            itemIds[i] = Long.parseLong(pId);
            i++;
        }
        int result = cartService.deleteCartItems(member.getId(), itemIds);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/member/delCartChecked", method = RequestMethod.POST)
    @ApiOperation(value = "删除购物车选中商品")
    public Result<Object> delChecked(String token) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        cartService.delChecked(member.getId());
        return new ResultUtil<Object>().setData(null);
    }
}
